//
//  TJViewController.m
//  Food
//
//  Created by dlios on 15/7/17.
//  Copyright (c) 2015年 李志远. All rights reserved.
//

#import "TJViewController.h"
#import "TJCell.h"
@interface TJViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,MBProgressHUDDelegate>
@property (nonatomic, retain) UICollectionView *myCollectionView;
@property (nonatomic, retain) NSMutableArray *mainArr;
@property (nonatomic, retain) MBProgressHUD *progress;

@end

@implementation TJViewController

- (void)dealloc
{
    [super dealloc];
    [_myCollectionView release];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"推荐菜";
    [self getData];
    [self createCollection];
    
}

- (void)createProgress
{
    self.progress = [[MBProgressHUD alloc] initWithView:self.view];
    [self.progress show:YES];
    self.progress.labelText = @"稍等";
    self.progress.detailsLabelText = @"正在加载...";
    [self.view addSubview:self.progress];
    
}

- (void)hudWasHidden:(MBProgressHUD *)hud
{
    [_progress removeFromSuperview];
    [_progress release];
}

- (void)getData
{
    Reachability *r = [Reachability reachabilityWithHostName:@"www.baidu.com"];
    if (r.currentReachabilityStatus == NotReachable) {
        UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网络连接失败" delegate:self cancelButtonTitle:@"好" otherButtonTitles:nil];
        [alert1 show];
        
    }else{

    NSString *str = @"http://api.meishixing.com/place/food/foodlist/place_id=";
    NSString *str1 = @"&page=1";
    NSString *url = [NSString stringWithFormat:@"%@%@%@",str,self.placeID,str1];
    
    self.mainArr = [NSMutableArray array];
    [AFNet connectNetGetDataWithURLString:url parameters:nil finishblock:^(id result) {
        [self.progress hide:YES];
        NSMutableArray *arr = [result objectForKey:@"result"];
        for (NSDictionary *dic in arr) {
            TJModel *model = [[TJModel alloc] initWithDic:dic];
            [self.mainArr addObject:model];
            [model release];
        }
        [self.myCollectionView reloadData];
    }];
        
        [self createProgress];
    }
    
}


- (void)createCollection
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(140, 170);
    
    self.myCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64-49) collectionViewLayout:layout];
    self.myCollectionView.backgroundColor = RGBCOLOR(239, 248, 237);
    self.myCollectionView.contentInset = UIEdgeInsetsMake(10, 10, 20, 10);
    self.myCollectionView.dataSource = self;
    self.myCollectionView.delegate = self;
    [self.myCollectionView registerClass:[TJCell class] forCellWithReuseIdentifier:@"cell"];
    [self.view addSubview:self.myCollectionView];
    [_myCollectionView release];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.mainArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    TJCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.backgroundColor = RGBCOLOR(239, 248, 237);
    TJModel *model = self.mainArr[indexPath.item];
    cell.model = model;
    return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
